<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="员工姓名">
        <el-input v-model="dataForm.username" clearable placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="生日餐状态" prop="state">
        <el-select v-model="dataForm.state" clearable placeholder="请选择" style="width: 100%">
          <el-option label="已订餐" value="1"></el-option>
          <el-option label="已取餐" value="2"></el-option>
          <el-option label="未取餐" value="3"></el-option>
          <el-option label="已取消" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订餐日期">
        <el-date-picker v-model="dataForm.range" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <!-- <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
      :header-cell-style="{background:'#f2f2f2',color:'#666'}">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column prop="id" header-align="center" align="center" width="80" label="ID"></el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="员工姓名" width="120"></el-table-column>
      <el-table-column prop="number" header-align="center" align="center" label="工号" width="80"></el-table-column>
      <el-table-column prop="telnum" header-align="center" align="center" label="手机号" width="120"></el-table-column>
      <el-table-column prop="birthday" header-align="center" align="center" label="职工生日" width="120"></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="下单时间"></el-table-column>
      <el-table-column prop="orderTime" header-align="center" align="center" label="订餐时间">
          <template slot-scope="scope">
          <div size="small" type="warning">{{scope.row.orderTime}} 11:00-12:00</div>
        </template>
      </el-table-column>
      <el-table-column prop="state" header-align="center" align="center" label="生日餐状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 1" size="small" type="warning">已订餐</el-tag>
          <el-tag v-if="scope.row.state === 2" size="small" type="success">已取餐</el-tag>
          <el-tag v-if="scope.row.state === 3" size="small" type="info">未取餐</el-tag>
          <el-tag v-if="scope.row.state === 4" size="small" type="danger">已取消</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="takeTime" header-align="center" align="center" label="取餐时间"></el-table-column>
      <el-table-column prop="vname" header-align="center" align="center" label="出餐人" width="120"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="lookDetailsInfo(scope.row)">员工详情</el-button>
          <el-button size="small" type="text" v-if="scope.row.state!=2 && scope.row.state!=4" @click="cancelHandle(scope.row.id)">取消订餐</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

     <!-- 详情对话框 -->
    <el-dialog :visible.sync="detailsModel" title="详情">
      <el-form :data="detailInfo">
        <el-form-item label="员工姓名:">
          <span>{{ detailInfo.username}}</span>
        </el-form-item>
        <el-form-item label="工号:">
          <span>{{ detailInfo.number }}</span>
        </el-form-item>
        <el-form-item label="手机号:">
          <span>{{ detailInfo.telnum }}</span>
        </el-form-item>
        <el-form-item label="职工生日	:">
          <span>{{ detailInfo.birthday }}</span>
        </el-form-item>
        <el-form-item label="下单时间	:">
          <span>{{ detailInfo.createTime }}</span>
        </el-form-item>
        <el-form-item label="订餐时间	:">
          <span>{{ detailInfo.orderTime }}</span>
        </el-form-item>
        <el-form-item label="生日餐状态	:">
           <div>
            <el-tag v-if="detailInfo.state === 1" size="small" type="warning">已订餐</el-tag>
            <el-tag v-if="detailInfo.state === 2" size="small" type="success">已取餐</el-tag>
            <el-tag v-if="detailInfo.state === 3" size="small" type="info">未取餐</el-tag>
            <el-tag v-if="detailInfo.state === 4" size="small" type="danger">已取消</el-tag>
          </div>
        </el-form-item>
        <el-form-item label="取餐时间	:">
          <span>{{ detailInfo.takeTime }}</span>
        </el-form-item>
        <el-form-item label="出餐人	:">
          <span>{{ detailInfo.vname }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
// import AddOrUpdate from './meal-add-or-update'
export default {
  data() {
    return {
      dataForm: {
        username: '',
        range: '',
        state:null
      },
      dataListSelections: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      detailsModel:false,
      detailInfo:{}
    }
  },
  components: {
    // AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    getDataList() {
      this.dataListLoading = true
      let startTime = null
      let endTime = null
      if (this.dataForm.range && this.dataForm.range.length > 1) {
        startTime = this.dataForm.range[0];
        endTime = this.dataForm.range[1];
      }
      this.$http({
        url: this.$http.adornUrl('/meal/list'),
        method: 'get',
          params: this.$http.adornParams({
          page: this.pageIndex.toString(),
          limit: this.pageSize.toString(),
          username: this.dataForm.username,
          state: this.dataForm.state,
          startTime: startTime,
          endTime: endTime,
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 查看详情模态框
    lookDetailsInfo(row) {
      this.detailsModel = true
      this.detailInfo = row;
    },
    // 取消订餐
    cancelHandle(id){
      this.$confirm(`确定取消订餐?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl('/meal/cancel'),
              method: "get",
              params: this.$http.adornParams({
                id:id
              })
            }).then(({ data }) => {
              if (data && (data.code === 0 || data.code === '0')) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
        })
      .catch(() => {})
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/article/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && (data.code === 0 || data.code === '0')) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped>
</style>
